<?php

/*----------------------------------------------+
|  MaxForum					|
|  ===========================================	|
|  By Majd Almontaser				|
|  Released under the License GNU v3.0		|
|  http://www.Max4Dev.com			|
|  ===========================================	|
|  Ttmtt Team - http://www.liioiil.com		|
+-----------------------------------------------*/

function rating_bar($id, $units = 0, $static='')
{
	global $db_prefix, $my_id, $lang;

	$my_address		="http://".$_SERVER['HTTP_HOST']."".$_SERVER['PHP_SELF'];
	$site_address 	= htmlspecialchars(str_replace('/index.php', '', $my_address));
	
	$rating_unitwidth = 25;
		
	//set some variables
	if (!$units) {$units = 10;}
	if (!$static) {$static = FALSE;}

	// get votes, values, ips for the current rating bar
	$query = mysql_query('SELECT total_votes, total_value, users FROM '.$db_prefix.'gallery_images_ratings WHERE image = ' . $id);

	$numbers = mysql_fetch_assoc($query);

	if ($numbers['total_votes'] < 1)
	
		$count = 0;
	
	else
	
		$count = $numbers['total_votes']; //how many votes total
	
	$current_rating = $numbers['total_value']; //total number of rating added together and stored

	#----------------------------------------------------
	# determine whether the user has voted,
	# so we know how to draw the ul/li
	#----------------------------------------------------

		$users = explode(',', $numbers['users']);
		
		if (in_array($my_id, $users))
		
			if ($static == '') $voted = true;
	
	// now draw the rating bar
	if ($count != 0)
	{
		$rating_width	= number_format($current_rating / $count, 2) * $rating_unitwidth;
		$rating1		= number_format($current_rating / $count, 1);
		$rating2		= number_format($current_rating / $count, 2);
	}
	
	$class = 'unit-rating';
		if ($static == 'static3') $class .= ' unit-rating2';

		$static_rater = array();

	if ($static == 'static' || $static == 'static2' || $static == 'static3')
	{
	
		$static_rater[] .= "\n".'<div class="ratingblock">';
		$static_rater[] .= '<div id="unit_long'.$id.'">';
		$static_rater[] .= '<ul id="unit_ul'.$id.'" class="' . $class . '" style="width:'.$rating_unitwidth*$units.'px;">';
		$static_rater[] .= '<li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';
		$static_rater[] .= '</ul>';
		
		if ($static == 'static')
		
			$static_rater[] .= '<p class="static">' . sprintf($lang['view_image_votes_cast'], $count) . '</p>';
		
		$static_rater[] .= '</div>';
		$static_rater[] .= '</div>'."\n\n";

		return join("\n", $static_rater);


	}
	else
	{

	  $rater ='';
	  $rater.='<div class="ratingblock">';

	  $rater.='<div id="rater_div">';
	  $rater.='  <ul id="unit_ul'.$id.'" class="unit-rating unit-rating2" style="width:'.$rating_unitwidth*$units.'px;">';
	  $rater.='     <li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';

	  for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
		   if(!$voted) { // if the user hasn't yet voted, draw the voting stars
			  $rater.='<li><a href="' . $site_address . '/includes/pages/gallery/rater/db.php?j='.$ncount.'&amp;q='.$id.'&amp;t='.$ip.'&amp;c='.$units.'" title="'.$ncount.' out of '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
		   }
	  }
	  $ncount=0; // resets the count

	  $rater.='  </ul>';
	  $rater.='  <p';
	  if($voted){ $rater.=' class="voted"'; }
	  $rater.='>' . sprintf($lang['view_image_votes_cast'], $count);
	  if ($voted) $rater .= '&nbsp;' .$lang['view_image_already_voted'];
	  $rater.='  </p>';
	  $rater.='</div>';
	  $rater.='</div>';
	  return $rater;
	 }
}
?>
